package cc.telecomdigital.tdstock.activity.groups.more;

import android.content.SharedPreferences;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.BaseActivity;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;

public class PhoneBettingBaseActivity extends BaseActivity
{

	private final static String	TAG		= PhoneBettingBaseActivity.class
											.getSimpleName();

	protected String[]			languages;

	private View				lastCheckedView;

	protected String[]			phoneBettingContent;

	protected String[]			phoneBettingNumber;

	protected int[]			rowIds;

	private int[]				itemIds	= new int[] { R.id.cantonese,
			R.id.putonghua, R.id.english	};

	private int[]				rowBgIds	= new int[] {
			R.drawable.first_item_bg, R.drawable.ordinary_item_bg,
			R.drawable.last_item_bg		};

	protected int				currentIndex;

	protected SharedPreferences	sharedPreferences;

	protected String			keyPhoneNumber;

	protected String			keyCurrentIndex;

	protected void initList()
	{

		for (int j = 0; j < itemIds.length; j++)
		{

			ViewGroup convertView = (ViewGroup) findViewById(itemIds[j]);

			TextView language = (TextView) convertView
					.findViewById(R.id.language);

			language.setText(languages[j]);

			for (int i = 0, len = rowIds.length; i < len; i++)
			{

				View row = convertView.findViewById(rowIds[i]);

				TextView content = (TextView) row
						.findViewById(R.id.content);

				TDStockLog.d(TAG, "position * len + i:" + (j * len + i));

				TDStockLog.d(TAG, "phoneBettingContent[position * len + i]:"
						+ phoneBettingContent[j * len + i]);

				content.setText(phoneBettingContent[j * len + i]);

				//第一行和最后一行的背景不一样
				if (i == 0)
				{

					row.setBackgroundResource(rowBgIds[0]);
				}
				else if (i == len - 1)
				{

					row
							.setBackgroundResource(rowBgIds[rowBgIds.length - 1]);
				}

				final int index = j * len + i;

				if (index == currentIndex)
				{

					lastCheckedView = row.findViewById(R.id.isChecked);

					lastCheckedView.setVisibility(View.VISIBLE);
				}

				row.setOnClickListener(

				new View.OnClickListener()
				{

					@Override
					public void onClick(View v)
					{
						// TODO Auto-generated method stub

						if (index == currentIndex)
						{//如果当前已经被选中，则不做其他操作

							return;
						}

						if (!updateSetting(index))
						{//如果保存设置不成功，则不做其他操作

							return;
						}

						View checkedView = v.findViewById(R.id.isChecked);

						checkedView.setVisibility(View.VISIBLE);

						lastCheckedView.setVisibility(View.INVISIBLE);

						lastCheckedView = checkedView;

						currentIndex = index;

						TDStockLog.d(TAG, "currentIndex:" + currentIndex);

						TDStockLog.d(TAG, "currentIndexPhoneNumber:"
								+ phoneBettingNumber[currentIndex]);
					}
				});

			}

		}
	}

	//更新设置,index为当前选中项的索引
	private boolean updateSetting(int index)
	{

		SharedPreferences.Editor editor = sharedPreferences.edit();

		editor.putString(keyPhoneNumber, phoneBettingNumber[index]);

		editor.putInt(keyCurrentIndex, index);

		return editor.commit();

	}
	
	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}
	

}